/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.cncqs.backend.company.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.cncqs.backend.common.service.CommonService;
import com.cncqs.backend.company.entity.CheckDTO;
import com.jeesite.modules.file.entity.FileEntity;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.dao.EmployeeDao;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.service.EmployeeService;
import com.jeesite.modules.sys.utils.UserUtils;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.cncqs.backend.company.entity.ComCompany;
import com.cncqs.backend.company.dao.ComCompanyDao;

/**
 * 企业管理Service
 * @author cxy
 * @version 2019-04-16
 */
@Service
@Transactional(readOnly=true)
public class ComCompanyService extends CrudService<ComCompanyDao, ComCompany> {

	@Value("${imageBaseUrl}")
	private String imageBaseUrl;

	@Autowired
	private ComCompanyDao comCompanyDao;
	@Autowired
	private CommonService commonService;
	
	/**
	 * 获取单条数据
	 * @param comCompany
	 * @return
	 */
	@Override
	public ComCompany get(ComCompany comCompany) {
		return super.get(comCompany);
	}
	
	/**
	 * 查询分页数据
	 * @param comCompany 查询条件
	 * @return page 分页对象
	 */
	@Override
	public Page<ComCompany> findPage(ComCompany comCompany) {
		Page<ComCompany> page = super.findPage(comCompany);
		List<ComCompany> list = page.getList();
		for (ComCompany entity : list) {
			List<FileUpload> businessPictureUrl = FileUploadUtils.findFileUpload(entity.getId(), "businessPicture");
			if (businessPictureUrl != null && businessPictureUrl.size() > 0) {
				FileEntity fileEntity = businessPictureUrl.get(0).getFileEntity();
				entity.setBusinessPicture(imageBaseUrl + fileEntity.getFilePath() + fileEntity.getFileId() + "." + fileEntity.getFileExtension());
			}

			List<FileUpload> licensePictureUrl = FileUploadUtils.findFileUpload(entity.getId(), "licensePicture");
			if (licensePictureUrl != null && licensePictureUrl.size() > 0) {
				FileEntity fileEntity = licensePictureUrl.get(0).getFileEntity();
				entity.setLicensePicture(imageBaseUrl + fileEntity.getFilePath() + fileEntity.getFileId() + "." + fileEntity.getFileExtension());
			}

			List<FileUpload> idPictureUrl = FileUploadUtils.findFileUpload(entity.getId(), "idPicture");
			if (idPictureUrl != null && idPictureUrl.size() > 0) {
				FileEntity fileEntity = idPictureUrl.get(0).getFileEntity();
				entity.setIdPicture(imageBaseUrl + fileEntity.getFilePath() + fileEntity.getFileId() + "." + fileEntity.getFileExtension());
			}

		}
		return page;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param comCompany
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(ComCompany comCompany) {
		FileUploadUtils.saveFileUpload(comCompany.getId(), "businessPicture");
		FileUploadUtils.saveFileUpload(comCompany.getId(), "licensePicture");
		FileUploadUtils.saveFileUpload(comCompany.getId(), "idPicture");

		Employee employee = comCompany.getEmployee();
		Map emp = commonService.queryOfficeEmpCodeByOfficeCode(employee.getOffice().getOfficeCode());
		comCompany.setRefCode(emp.get("empCode").toString());
		comCompany.setRefName(emp.get("officeName").toString());
		super.save(comCompany);
	}
	
	/**
	 * 更新状态
	 * @param comCompany
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(ComCompany comCompany) {
		super.updateStatus(comCompany);
	}
	
	/**
	 * 删除数据
	 * @param comCompany
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(ComCompany comCompany) {
		super.delete(comCompany);
	}

	/**
	 * 根据公司id，查询其审核历史记录
	 * */
	public List<CheckDTO> queryCheckDetailByCompanyId(Long companyId){
		return comCompanyDao.queryCheckDetailByCompanyId(companyId);
	}

	/**
	 * 保存审核记录
	 * */
	@Transactional(rollbackFor = Exception.class)
	public void saveCheck(ComCompany comCompany) {
		comCompany.setCheckStatus(comCompany.getCheckStatus() == 0 ? 2 : comCompany.getCheckStatus());
		comCompany.setOperator(UserUtils.getUser().getUserName());
		comCompany.setCreateBy(UserUtils.getUser().getLoginCode());
		comCompanyDao.saveCheckRecord(comCompany);
		super.save(comCompany);
	}

	public Long getIsWorkDayCodeByDate() {
		return comCompanyDao.getIsWorkDayCodeByDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
	}

	
}